package jmine.tec.persist.schema.impl.dialects;

import java.sql.Types;
import java.util.LinkedList;
import java.util.List;

import jmine.tec.persist.schema.api.SchemaColumn;

/**
 * Dialect para HSQL
 * 
 * @author lundberg
 */
public class HSQLDialect extends AbstractSqlDialect {

    private static final int MAX_TINY = 3;

    private static final int MAX_SMALL = 5;

    private static final int MAX_INTEGER = 10;

    private static final int MAX_BIGINT = 21;

    /**
     * Construtor
     */
    public HSQLDialect() {
        super(new org.hibernate.dialect.HSQLDialect());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String renderType(int code, int length, int precision, int scale) {
        if (Types.NUMERIC == code && scale == 0) {
            if (precision < MAX_TINY) {
                return super.renderType(Types.TINYINT, length, precision, scale);
            } else if (precision < MAX_SMALL) {
                return super.renderType(Types.SMALLINT, length, precision, scale);
            } else if (precision < MAX_INTEGER) {
                return super.renderType(Types.INTEGER, length, precision, scale);
            } else if (precision < MAX_BIGINT) {
                return super.renderType(Types.BIGINT, length, precision, scale);
            } else {
                return super.renderType(code, length, precision, scale);
            }
        } else {
            return super.renderType(code, length, precision, scale);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected List<String> renderIdentityPrimaryKey(String table, String name, SchemaColumn column) {
        List<String> result = new LinkedList<String>();
        result.add(String.format("ALTER TABLE %s ADD CONSTRAINT %s PRIMARY KEY (%s)", table, name, column.getName()));
        result.add(String.format("ALTER TABLE %s ALTER COLUMN %s BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 100000)", table,
                column.getName()));
        return result;
    }
}
